package com.yz.conroller;

import com.yz.core.utils.R;
import com.yz.security.UserTokenThreadLocal;
import com.yz.service.impl.SysLoginServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @className: SysLoginController
 * @author: shuishui
 * @date: 2023/01/03 20:02
 */
@RestController
public class SysLoginController {
    @Autowired
    private SysLoginServiceImpl sysLoginService;

    @PostMapping("/login")
    public R login(@RequestBody Map<String,String> param){
        String username = param.get("username");
        String password = param.get("password");
        if (ObjectUtils.isEmpty(username)){
            return R.error("用户名不能为空");
        }
        if (ObjectUtils.isEmpty(password)){
            return R.error("密码不能为空");
        }
        String token = sysLoginService.login(username, password);
        return R.ok().put("token",token);
    }
    @GetMapping("/info")
    public R info() {
        return R.ok().put("data", UserTokenThreadLocal.get());
    }
    @GetMapping("/logout")
    public R logout(){
        sysLoginService.logout();
        return R.ok();
    }

}
